﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Src="SupplierInfo.ascx" TagName="SupplierInfo" TagPrefix="uc1" %>

<%@ Import Namespace="Buffalo.SMECommon.DAL" %>
<%@ Import Namespace="Buffalo.SMECommon.Enum" %>
<%@ Import Namespace="Buffalo.Web.Areas.SMECommon" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <ext:XScript ID="XScript1" runat="server">
        <script type="text/javascript">
            var saveFormHandler = function () {
                var tabIndex = getCurrentTabIndex();
                if (tabIndex == 0) {    //customer info
                    #{SupplierInfoForm}.form.submit({ waitMsg: 'Saving...', params: { id: getID() }, success: successHandlerSave, failure: failureHandler });
                }
            };
            var saveAndCloseFormHandler = function () {
                var tabIndex = getCurrentTabIndex();
                if (tabIndex == 0) {    //customer info
                    #{SupplierInfoForm}.form.submit({ waitMsg : 'Saving...', params : { id : getID() }, success : successHandlerSaveClose, failure : failureHandler });
                }
            };
        </script>
    </ext:XScript>
    <script type="text/javascript">
        var dataChanged = false;
        var failureHandler = function (form, action) {
            var msg = "";
            if (action.failureType == "client" || (action.result && action.result.errors && action.result.errors.length > 0)) {
                msg = "Please check fields";
            } else if (action.result && action.result.extraParams.msg) {
                msg = action.result.extraParams.msg;
            } else if (action.response) {
                msg = action.response.responseText;
            }

            Ext.Msg.show({
                title: "Save Error",
                msg: msg,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        };
        var successHandlerSaveClose = function (form, action) {
            successHandlerSave(form, action);
            hideSupplierEditorWindow();
        };
        var getCurrentTabIndex = function () {
            return SupplierTabPanel.items.indexOf(SupplierTabPanel.getActiveTab());
        };
        var successHandlerSave = function (form, action) {
            var tabIndex = getCurrentTabIndex();

            if (tabIndex == 0) {    //customer info

                if (action.result && action.result.extraParams && action.result.extraParams.newID) {
                    dsSupplier.getAt(0).id = action.result.extraParams.newID;
                    if (dsSupplier.getAt(0).newRecord) {
                        delete dsSupplier.getAt(0).newRecord;
                    }
                } else {
                    Ext.MessageBox.alert("Success", "Supplier has been saved!");
                }

                dataChanged = true;

                if (action.options.params.setNew) {
                    SupplierInfoForm.form.reset();
                    dsSupplier.removeAll();

                    var rec = new dsSupplier.recordType();
                    rec.newRecord = true;
                    dsSupplier.add(rec);
                }
            }
        };

        var getID = function () {
            var IDSupplier = (dsSupplier.getCount() > 0 && !dsSupplier.getAt(0).newRecord) ? dsSupplier.getAt(0).id : "";
            return IDSupplier;
        };

        var hideSupplierEditorWindow = function () {
            parent.SupplierEditorWindow.hide();
            if (dataChanged)
                parent.dsSupplier.reload();
        };

        var customerLoaded = function (store, records) {
            if (records.length > 0) {
                SupplierInfoForm.form.loadRecord(records[0]);

            } else {
                SupplierInfoForm.form.reset();
            }

            SupplierTabPanel.el.unmask();
        };

        var commandHandler = function (cmd, record) {
            switch (cmd) {
                case "delete":
                    Ext.Msg.confirm('Alert', 'Delete Supplier?', function (btn) {
                        if (btn == "yes") {
                            dsSupplier.remove(record);
                            dsSupplier.save();
                        }
                    });
                    break;
            }
        };
       
    </script>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Buffalo.Web.Areas.SMECommon.GeneralVar.gLanguageHelper.ApplyLanguage(this.mainViewPort, Buffalo.Web.Areas.SMECommon.CommonManager.GetCurrentCulture(), true);
        }
    </script>
</head>
<body>
    <ext:ResourceManager ID="ResourceManager1" runat="server">
    </ext:ResourceManager>
    <ext:Store ID="dsPartnerCategory" runat="server" RemoteSort="true" UseIdConfirmation="false" AutoLoad="true">
        <Proxy>
            <ext:HttpProxy Url="/SMECommon/Catalog/GetListPartnerCategory/" />
        </Proxy>
        <UpdateProxy>
            <ext:HttpWriteProxy Url="/SMECommon/Catalog/SavePartnerCategories/" />
        </UpdateProxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="SortNo" />
                    <ext:RecordField Name="Code" />
                    <ext:RecordField Name="Name" />
                    <ext:RecordField Name="Note" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="limit" Value="15" Mode="Raw" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="dir" Value="ASC" />
            <ext:Parameter Name="sort" Value="Name" />
            <ext:Parameter Name="withDefaultItem" Value="true"  Mode="Value"/> 
            <ext:Parameter Name="cboCriteria" Value="" Mode="Value" />
            <ext:Parameter Name="txtSearchText" Value="" Mode="Value" />
        </BaseParams>
        <SortInfo Field="Name" Direction="ASC" />
    </ext:Store>

    <ext:Store ID="dsSupplier" runat="server">
        <Proxy>
            <ext:HttpProxy Url="/Partner/GetPartner/" />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ID" Root="data" TotalProperty="total">
                <Fields>
                    <ext:RecordField Name="ID" />
                    <ext:RecordField Name="PartnerType" />
                    <ext:RecordField Name="Code" />
                    <ext:RecordField Name="Name" />
                    <ext:RecordField Name="Address1" />
                    <ext:RecordField Name="Address2" />
                    <ext:RecordField Name="PhoneNo" />
                    <ext:RecordField Name="Email" />
                    <ext:RecordField Name="Fax" />
                    <ext:RecordField Name="Position" />
                    <ext:RecordField Name="Note" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <BaseParams>
            <ext:Parameter Name="IDPartner" Value='<%# ViewData["IDSupplier"] %>' Mode="Value" AutoDataBind="true" />
        </BaseParams>
        <Listeners>
            <BeforeLoad Handler="#{SupplierTabPanel}.el.mask('Loading customer...', 'x-mask-loading');" />
            <LoadException Handler="#{SupplierTabPanel}.el.unmask();" />
            <Load Fn="customerLoaded" />
        </Listeners>
    </ext:Store>

    <ext:Viewport ID="mainViewPort" runat="server" Layout="fit">
        <Items>
            <ext:Panel ID="Panel1" runat="server" Border="false" Layout="fit">
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button ID="btnSave" runat="server" Text="Save" Icon="Disk">
                                <Listeners>
                                    <Click Handler="saveFormHandler()" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnSaveAndClose" runat="server" Text="Save and Close" Icon="DiskBlack">
                                <Listeners>
                                    <Click Handler="saveAndCloseFormHandler()" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnClose" runat="server" Text="Close" Icon="DoorOut">
                                <Listeners>
                                    <Click Handler="hideSupplierEditorWindow();" />
                                </Listeners>
                            </ext:Button>
                            <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Items>
                    <ext:TabPanel ID="SupplierTabPanel" runat="server" Border="false" LayoutOnTabChange="true">
                        <Items>
                            <ext:Panel ID="panEmployeeInfo" runat="server" Title="EmployeeInfo" Padding="6" Layout="fit" AutoScroll="true">
                                <Content>
                                    <uc1:SupplierInfo ID="SupplierInfo1" runat="server" />
                                </Content>
                            </ext:Panel>
                        </Items>
                    </ext:TabPanel>
                </Items>

            </ext:Panel>
        </Items>
    </ext:Viewport>
</body>
</html>
